package com.alibaba.sdk.config.async;

import com.alibaba.ttl.TtlCallable;
import com.alibaba.ttl.TtlRunnable;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
 * 线程执行器
 *
 * @ author  Shawn(Shawn_0217@163.com)
 * @ date    2021/4/27 15:46
 * @ company Alibaba
 */
public class TtlThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {

    /**
     * 执行异步线程
     *
     * @param task
     */
    @Override
    public void execute(Runnable task) {
        Runnable ttlRunnable = TtlRunnable.get(task);
        super.execute(ttlRunnable);
    }

    /**
     * 执行异步线程
     *
     * @param task
     * @param startTimeout
     */
    @Override
    public void execute(Runnable task, long startTimeout) {
        Runnable ttlRunnable = TtlRunnable.get(task);
        super.execute(ttlRunnable, startTimeout);
    }

    /**
     * 提交异步线程
     *
     * @param task
     * @return
     */
    @Override
    public Future<?> submit(Runnable task) {
        Runnable ttlRunnable = TtlRunnable.get(task);
        return super.submit(ttlRunnable);
    }

    /**
     * 提交异步线程
     *
     * @param task
     * @param <T> 泛型
     * @return
     */
    @Override
    public <T> Future<T> submit(Callable<T> task) {
        Callable<T> ttlRunnable = TtlCallable.get(task);
        return super.submit(ttlRunnable);
    }
}
